package com.orangelife.mobile.myhouse.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.constants.HouseURL;
import com.orangelife.mobile.myhouse.adapter.SelectLocationLvAdapter;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.myhouse.entity.LocationBean;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * 已开通城市列表
 * Created by 吴格非 on 2015/11/14.
 */
public class SelectHouseLocationActivity extends Activity {

    @Bind(R.id.lvHouseLocation)
    ListView lvHouseLocation;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;

    public static final String TYPE_CITY = "city";
    public static final String TYPE_COMM = "comm";
    public static final String TYPE_BUILDING = "building";
    public static final String TYPE_ROOM_NUM = "district";
    public static final String INTENT_PRE_VALUE = "INTENT_PRE_VALUE";
    public static final String INTENT_SELECT_TYPE = "INTENT_SELECT_TYPE";
    public static final String INTENT_RESULT = "INTENT_RESULT";
    private String currentType;
    private String preValue;
    private List<LocationBean> mList = new ArrayList<>();
    private SelectLocationLvAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_location);
        ButterKnife.bind(this);
        initIntentValue();
        requestList();
        setAdapter();
        initTitle();
        CurryApplication.getInstance().addActivity(this);
        setListener();
    }

    private void setListener() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestList();
            }
        });
    }

    private void initIntentValue() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            preValue = bundle.getString(INTENT_PRE_VALUE);
            currentType = bundle.getString(INTENT_SELECT_TYPE);
        }
    }

    private void setAdapter() {
        adapter = new SelectLocationLvAdapter(this, mList);
        lvHouseLocation.setAdapter(adapter);
    }

    private String getURL() {
        String url = null;
        switch (currentType) {
            case TYPE_CITY:
                url = HouseURL.CITY;
                break;
            case TYPE_COMM:
                url = HouseURL.COMM;
                break;
            case TYPE_BUILDING:
                url = HouseURL.BUILDING.replace("{comID}", StringUtil.encode(preValue));
                break;
            case TYPE_ROOM_NUM:
                url = HouseURL.APARTMENT.replace("{buildID}", StringUtil.encode(preValue));
                break;
        }
        return url;
    }

    private String getCityName() {
        if (TYPE_COMM.equals(currentType)) {
            return preValue;
        }
        return null;
    }

    private void requestList() {
        HouseHttpBiz.getLocationList(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                emptyLayout.dismiss();
                mList = HouseHttpBiz.handleLocationList(response);
                if (mList != null && mList.size() > 0) {
                    adapter.setCurrentType(currentType);
                    adapter.setList(mList);
                    adapter.notifyDataSetChanged();
                } else {
                    emptyLayout.setErrorType(EmptyLayout.NODATA);
                }
            }
        }, getCityName(), getURL());
    }

    @OnItemClick(R.id.lvHouseLocation)
    public void onItemClick(int position) {
        LocationBean lb = mList.get(position);
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(INTENT_SELECT_TYPE, currentType);
        bundle.putSerializable(INTENT_RESULT, lb);
        intent.putExtras(bundle);
        setResult(0, intent);
        CurryApplication.getInstance().finishActivity(this);
    }

    /*##################标题栏start###################*/
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;

    @OnClick({R.id.ib_title_left, R.id.iv_title_right})
    public void onClickTitle(View view) {
        switch (view.getId()) {
            case R.id.ib_title_left:
                CurryApplication.getInstance().finishActivity(this);
                break;
            case R.id.iv_title_right:
                break;
        }
    }

    private void initTitle() {
        tvTitle.setText("选择区域");
    }

    public void onBackClick(View view) {
        CurryApplication.getInstance().finishActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CurryApplication.getInstance().finishActivity(this);
    }
}
